// Manuscript: "How People Choose Between Criminal Opportunities" 
// Authors: Andrew T. Krajewski, UT Dallas; Justin T. Pickett, University at Albany, SUNY; Bruce Jacobs, Criminology Consultant
// Date: 05-27-2025

// "Estout" functions for creating tables
* net install st0085_2.pkg 

// Loading in the data
use "CrimeOpportunitiesConj-ReplicationData.dta", clear

// Average marginal component effects (AMCEs) of opportunity attributes on selection (Figure 1 and Online Supplement C)
regress cj1_chosen i.victim i.peopleinvolved i.completiontime i.yourpayout i.timeuntilpayout i.chanceofarrest i.possiblepunishment, cluster(id) cformat(%5.3f)
eststo cj1_q1

esttab cj1_q1, replace title("Average marginal component effects (AMCEs) of opportunity attributes on selection") mtitles("Binary Choice") label b(3) se(3) wide stardetach nolines nogaps nodepvars nonumbers

// Disaggregation of average marginal component effects (AMCEs) by moderators (Figure 2 and Online Supplement D)
// Self-control
sum selfcontrol_avg, detail
global sc50 = r(mean)
regress cj1_chosen i.victim i.peopleinvolved i.completiontime i.yourpayout i.timeuntilpayout i.chanceofarrest ///
	i.possiblepunishment if selfcontrol_avg <= $sc50, cluster(id) cformat(%5.3f) // Low self-control
eststo cj1_selfc1 
regress cj1_chosen i.victim i.peopleinvolved i.completiontime i.yourpayout i.timeuntilpayout i.chanceofarrest ///
	i.possiblepunishment if selfcontrol_avg > $sc50 , cluster(id) cformat(%5.3f) // High self-control
eststo cj1_selfc2

esttab cj1_selfc1 cj1_selfc2, replace title("Disaggregation of average marginal component effects (AMCEs) by self-control") mtitles("Low Self-Control" "High Self-Control") label b(3) se(3) wide stardetach nolines nogaps nodepvars nonumbers

// Previous offending
regress cj1_chosen i.victim i.peopleinvolved i.completiontime i.yourpayout i.timeuntilpayout i.chanceofarrest ///
	i.possiblepunishment if offenderD == 0, cluster(id) cformat(%5.3f) // Non-offenders
eststo cj1_offender1
regress cj1_chosen i.victim i.peopleinvolved i.completiontime i.yourpayout i.timeuntilpayout i.chanceofarrest ///
	i.possiblepunishment if offenderD == 1, cluster(id) cformat(%5.3f) // Offenders
eststo cj1_offender2

esttab cj1_offender1 cj1_offender2, replace title("Disaggregation of average marginal component effects (AMCEs) by prior offending") mtitles("Non-Offender" "Offender") label b(3) se(3) wide stardetach nolines nogaps nodepvars nonumbers 

// Average treatment moderation effects (ATME) (Table 2)
gen moderator = "selfcontrol_avg" if id == 1 
replace moderator = "offenderD" if id == 2

// Lines 42 through 73 must be run together.
local variables = "selfcontrol_avg offenderD attentioncheck"
levelsof moderator, local(levels) 

foreach k of local levels {
	local abbrev = "`k'"
	matrix int_`abbrev' = .,.,.,.,.,. 

	foreach i of varlist victimD victim_indiv victim_biz peopleinvolvedD peopleinvolved_group ///
	completiontime yourpayout timeuntilpayoutD chanceofarrest possiblepunishmentD possiblepunishment {
		quietly: su `i'
		local min = r(min)
		quietly: regress cj1_chosen `variables' if `i' == `min', cluster(id)
		local b1 = _b[`k']
		local se1 = _se[`k']
		quietly: su `i'
		local max = r(max)
		quietly: regress cj1_chosen `variables ' if `i' == `max', cluster(id)
		local b2 = _b[`k']
		local se2 = _se[`k']

		// Paternoster Approach to Significance
		local diff = `b2' - `b1'
		local se1_sq = `se1' * `se1'
		local se2_sq = `se2' * `se2'
		local se_sqrt = sqrt(`se1_sq' + `se2_sq')
		local z_score = `diff' / `se_sqrt'
		local p_value = 2*(1 - normal(abs(`z_score')))

		// Appending results to this matrix
		matrix int_`abbrev' = int_`abbrev' \ `b1' , `b2' , `diff' , `se_sqrt', `z_score', `p_value' 
	}
}

matrix dir

matrix colnames int_selfcontrol_avg = B1 B2 Difference StdError Z-Score P-Value
matrix rownames int_selfcontrol_avg = null victimD victim_indiv victim_biz peopleinvolvedD peopleinvolved_group ///
	completiontime yourpayout timeuntilpayoutD chanceofarrest possiblepunishmentD possiblepunishment
matrix list int_selfcontrol_avg, format(%5.3f) // Self-control

matrix colnames int_offenderD = B1 B2 Difference StdError Z-Score P-Value
matrix rownames int_offenderD = null victimD victim_indiv victim_biz peopleinvolvedD peopleinvolved_group ///
	completiontime yourpayout timeuntilpayoutD chanceofarrest possiblepunishmentD possiblepunishment
matrix list int_offenderD, format(%5.3f) // Previous offending
